<template>
  <view>
    <!-- 筛选 -->
    <view class="filter">
      <text class="active">综合</text>
      <text>销量</text>
      <text>价格</text>
    </view>
    <!-- 商品列表 -->
    <scroll-view class="goods" @scrolltolower="getMore" scroll-y>
      <!-- 遍历 -->
      <view
        class="item"
        v-for="item in list"
        :key="item.goods_id"
        @click="goDetail(item.goods_id)"
      >
        <!-- 商品图片 -->
        <image class="pic" :src="item.goods_small_logo" />
        <!-- 商品信息 -->
        <view class="meta">
          <view class="name">{{ item.goods_name }}</view>
          <view class="price">
            <text>￥</text>{{ item.goods_price }}
            <text>.00</text>
          </view>
        </view>
      </view>
      <!-- 列表加载完成显示 -->
      <view class="nomore" v-if="nomore">没有更多数据...</view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data () {
    // 存储渲染相关数据
    return {
      // 列表
      list: [],
      query: {
        pagenum: 1,
        pagesize: 20,
        query: ''
      },
      total: 0
    }
  },
  onLoad (params) {
    this.query.query = params.query
    this.getList(this.query)
  },
  computed: {
    // 是否加载完成
    nomore () {
      return this.total === this.list.length
    }
  },
  methods: {
    getMore () {
      if (this.nomore) return
      this.query.pagenum++
      console.log(this.query.pagenum)
      this.getList(this.query)
    },
    async getList (data) {
      const { msg, data: _d } = await this.request({
        url: "/api/public/v1/goods/search",
        data
      })
      this.total = _d.total
      if (msg.status === 200) {
        this.list.push(..._d.goods)
      }
    },
    goDetail (id) {
      uni.navigateTo({
        url: `/packone/goods/index?id=${id}`
      })
    }
  }
};
</script>

<style scoped lang="scss">
.nomore {
  font-size: 32rpx;
  color: rgb(167, 166, 166);
  margin: 30rpx auto;
  text-align: center;
}
.filter {
  display: flex;
  height: 96rpx;
  line-height: 96rpx;
  border-bottom: 1rpx solid #ddd;

  /* #ifdef H5 */
  position: relative;
  z-index: 99;
  /* #endif */

  text {
    flex: 1;
    text-align: center;
    font-size: 30rpx;
    color: #333;

    &.active {
      color: #ea4451;
    }
  }
}

.goods {
  position: absolute;
  width: 100%;
  top: 97rpx;
  bottom: 0;
}

.item {
  display: flex;
  padding: 30rpx 20rpx 30rpx 0;
  margin-left: 20rpx;
  border-bottom: 1rpx solid #eee;

  &:last-child {
    border-bottom: none;
  }

  .pic {
    width: 200rpx;
    height: 200rpx;
    margin-right: 30rpx;
  }

  .meta {
    flex: 1;
    font-size: 27rpx;
    color: #333;
    position: relative;
  }

  .name {
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .price {
    position: absolute;
    bottom: 0;

    color: #ea4451;
    font-size: 33rpx;

    text {
      font-size: 22rpx;
    }
  }
}
</style>
